﻿using Microsoft.Xna.Framework;
using Roller.Animation;
using System;

namespace Roller.Collision
{
    /// <summary>
    /// Static class designed to detect simple rectangular collision and message when events happen.
    /// </summary>
    public static class DetectCollision
    {
        #region Methods

        /// <summary>
        ///  Method for testing for collision, ensuring the provided item is located within the designated screen.
        /// </summary>
        /// <param name="screen">A <see cref="Rectangle"/> object representing the view portal for the collision operation.</param>
        /// <param name="item">A <see cref="RenderedDie"/> representing the object to be tested against.</param>
        /// <returns>A <see cref="Vector2"/> object representing the resulting velocity vector of the object if a collision occurs.</returns>
        public static Vector2 ContainsObject( Rectangle screen, RenderedDie item )
        {
            var resultingVector = Vector2.Zero;
            var itemRectangle = new Rectangle( Convert.ToInt32( item.Position.X ), Convert.ToInt32( item.Position.Y ), Convert.ToInt32( item.ActualWidth ), Convert.ToInt32( item.ActualHeight ) );

            if ( !( screen.Contains( itemRectangle ) ) )
            {
                if ( item.Position.Y <= 0 || item.Position.Y >= screen.Height )
                    return CalculateReflectionVertical( item );
                else
                    return CalculateReflectionHorizontal( item );
            }
            else
            {
                return item.Velocity;
            }
        }

        private static Vector2 CalculateReflectionVertical( RenderedDie item )
        {
            return Vector2.Reflect( item.Velocity, new Vector2( 0, 1 ) );
        }

        private static Vector2 CalculateReflectionHorizontal( RenderedDie item )
        {
            return Vector2.Reflect( item.Velocity, new Vector2( 1, 0 ) );
        }

        #endregion
    }
}
